package ldkxingzhe.l365calendar.fragment;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.Calendar;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.activity.AllEditActivity;
import ldkxingzhe.l365calendar.utils.CalendarUtil;
import ldkxingzhe.l365calendar.utils.OSUtil;
import ldkxingzhe.l365calendar.view.MainLayoutContainer;
import ldkxingzhe.l365calendar.view.MonthView;

/**
 * 首页的我的日历Fragment
 * Created by xingzhe on 16/1/19.
 */
public class MainList extends Fragment implements MonthView.OnCellSelectListener {
    @SuppressWarnings("unused")
    private static String TAG = "MainList";

    FrameLayout calendarLayout;
    private TextView monthTitle;
    private TextView dayAfterLabel;
    private View btnAdd, addLayout;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View resultView = inflater.inflate(R.layout.fragment_main_list, container, false);
        initTitle(resultView.findViewById(R.id.title_layout));
        calendarLayout = (FrameLayout) resultView.findViewById(R.id.content);
        initCalendarLayout();
        initAddLayout(resultView.findViewById(R.id.add_layout));
        return resultView;
    }

    private void initAddLayout(View addLayout) {
        this.addLayout = addLayout;
        dayAfterLabel = (TextView) addLayout.findViewById(R.id.middle_text);
        btnAdd = addLayout.findViewById(R.id.add);
        btnAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onAddClick();
            }
        });
    }

    private void onAddClick() {
        Log.v(TAG, "onAddClick");
        startActivity(new Intent(getActivity(), AllEditActivity.class));
    }

    public void onMonthViewHeightChanged(int height){
//        Log.v(TAG, "onMonthViewHeightChanged, and height is " + height);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) addLayout.getLayoutParams();
        layoutParams.topMargin= height - 20;
        addLayout.setLayoutParams(layoutParams);
    }

    MainLayoutContainer mainLayoutContainer;
    private void initCalendarLayout() {
        mainLayoutContainer = new MainLayoutContainer(getActivity(), this);
        mainLayoutContainer.setOnCellSelectListener(this);
        calendarLayout.addView(mainLayoutContainer, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    }

    private void initTitle(View titleLayout) {
        monthTitle = (TextView) titleLayout.findViewById(R.id.month_title);
        LinearLayout weekLayout = (LinearLayout) titleLayout.findViewById(R.id.weeks);
        String[] weekNames = getResources().getStringArray(R.array.week_names);
        for(int i = 0; i < 7; i++){
            TextView textView = new TextView(getActivity());
            textView.setText(weekNames[i]);
            textView.setGravity(Gravity.CENTER);
            textView.setTextColor(Color.BLACK);
            textView.setTextSize(OSUtil.dp2px(4));
            textView.setTypeface(Typeface.SANS_SERIF);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT);
            params.weight = 1;
            weekLayout.addView(textView, params);
        }
    }

    @Override
    public void onCellSelect(MonthView monthView, Calendar calendar) {
        setMonthTitle(CalendarUtil.formatTime(calendar.getTimeInMillis(), CalendarUtil.DATE));
    }

    public void setMonthTitle(String title){
        monthTitle.setText(title);
    }
}
